//
//  PlayMusicViewController.m
//  仿酷我音乐
//
//  Created by mac on 15/11/19.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "PlayMusicViewController.h"

@interface PlayMusicViewController ()

@end

@implementation PlayMusicViewController

-(void)loadView{
    
    self.view = [[UIView alloc] init];
    self.view.layer.anchorPoint = CGPointMake(0.5, 1.0);
    self.view.backgroundColor = [UIColor grayColor];
    self.view.frame = [UIScreen mainScreen].bounds;
    
    ///判断识别手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    
    [self.view addGestureRecognizer:pan];
}


-(void)panGesture:(UIPanGestureRecognizer *) recognizer{
    
    CGPoint point = [recognizer translationInView:self.view];
    
    //转场动画
    CGAffineTransform transform = self.view.transform;
    
    //判断手势的状态
    if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
        
        NSLog(@"%@",NSStringFromCGAffineTransform(transform));
        //判断是否切换界面
        if (ABS(transform.b) > 0.25) {
            //切换界面
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [UIView animateWithDuration:0.25 delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity:10 options:0 animations:^{
                self.view.transform = CGAffineTransformIdentity;
            } completion:^(BOOL finished) {
                
            }];
        }
        
    }else{
    
        CGFloat viewAngle = atan2(transform.b, transform.a);
        
        CGFloat dx = point.x * cos(viewAngle);
        CGFloat dy = point.y * sin(viewAngle);
        
        CGFloat angle = (dx + dy) / self.view.bounds.size.width;
        
        transform = CGAffineTransformRotate(transform, angle);
        
        transform.tx += 2 * (dy + dx);
        
        self.view.transform = transform;
        
        [recognizer setTranslation:CGPointZero inView:self.view];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

@end
